package com.jushu.storbox.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NotificationCompat;

import com.jushu.storbox.LoginActivity;
import com.jushu.storbox.R;
import com.jushu.storbox.callback.IUploadCallback;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.filesystem.FileManagerFactory;
import com.jushu.storbox.filesystem.iFileManager;
import com.jushu.storbox.service.ICloudDriveService;
import com.huawei.sharedrive.sdk.android.modelV2.response.FileInfoResponseV2;
public class DataUploadUtil {
	private NotificationCompat.Builder mNotification; 
    private NotificationManager mNotificationManager; 
    private int notificationID;
    private String folderID;
    private Context context;
    private ICloudDriveService shareDriveService;
    public DataUploadUtil (Context context,ICloudDriveService shareDriveService)
    {
    	this.context = context;
    	this.shareDriveService = shareDriveService;
    	notificationID = ClientConfig.NOTIFICATION_ID +=1;
    	mNotificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    }
    @Deprecated
	public void uploadStream(final String folderID,final String uploadFileUrl,final Handler handler) 
	{
	    iFileManager mdmt = FileManagerFactory.getFileManager(context);
		if (!mdmt.fileIsExist(uploadFileUrl)) {
			handler.sendEmptyMessage(6);
			return;
		}
		final String name = mdmt.getFileName(uploadFileUrl);
		//
		final IUploadCallback uploadCallback = new IUploadCallback() {
			
			@Override
			public void onSuccess(FileInfoResponseV2 successItem) {
				Message message = new Message();
				message.what = 3;
				message.obj = uploadFileUrl;
				Bundle bundle = new Bundle();
				bundle.putString("folderID", folderID);
				bundle.putString("fileID", successItem.getId());
				bundle.putString("filePath", uploadFileUrl);
				message.setData(bundle);
				//
				mNotification.setContentText(name +"  "+    context.getResources().getString(R.string.fileListadapter_upload_success));
				mNotificationManager.notify(notificationID, mNotification.build());
				mNotificationManager.cancel(notificationID);
				handler.sendMessage(message);
			} 
			@Override
			public void onStart() {
				mNotification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon_logo)
						  .setContentTitle(name)
						  .setContentText(context.getResources().getString(R.string.upload_Notification_title));
						mNotification.setAutoCancel(false);
						mNotification.setTicker(context.getResources().getString(R.string.upload_Notification_title));
						Intent intent = new Intent(Intent.ACTION_MAIN);
						intent.addCategory(Intent.CATEGORY_LAUNCHER);
						intent.setClass(context, LoginActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
						PendingIntent p=PendingIntent.getActivity(context, 0, intent, 0);
						mNotification.setContentIntent(p);
						//
						mNotification.setProgress(100, (int)0, false);
						mNotification.setContentText(context.getResources().getString(R.string.upload_Notification_title)+"    "+(int)0+"%");
						mNotificationManager.notify(notificationID, mNotification.build());
					}
			@Override
			public void onProgress(int currentProgress, long currentSize, long maxSize) {
//				share_notification_opertaing_title
				mNotification.setContentTitle(name);
				mNotification.setProgress(100, (int)currentProgress, false);
				mNotification.setContentText(context.getResources().getString(R.string.upload_Notification_title)+"    "+(int)currentProgress+"%");
				mNotificationManager.notify(notificationID, mNotification.build());
			}
			@Override
			public void onFailure(Throwable t) {
				Message message = new Message();
				message.what = 4;
				handler.sendMessage(message);
				mNotification.setContentText(name +"  "+    context.getResources().getString(R.string.fileListadapter_upload_failed));
				mNotificationManager.notify(notificationID, mNotification.build());
				mNotificationManager.cancel(notificationID);
			}
			@Override
			public void onStop() {
			}
			@Override
			public void onCanceled() {
			}
			@Override
			public void onDettach() {
			}
		};

			Thread thread = new Thread() {
			@Override
			public void run() {
//				OutputStream outputStream = new FileOutputStream(uploadFileUrl)
//				File file = new File(uploadFileUrl);
				try {
					if (null != shareDriveService)
						shareDriveService.uploadFile(folderID ,uploadFileUrl,new FileInputStream(uploadFileUrl),uploadCallback);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				super.run();
			}
		};
		thread.start();
	}
	public void uploadStream(final String folderID,final String uploadFileUrl,final String fileEncPath,final InputStream inputStream,final Handler handler) 
	{
		iFileManager mdmt = FileManagerFactory.getFileManager(context, fileEncPath);
		if (!mdmt.fileIsExist(uploadFileUrl))
		{
			handler.sendEmptyMessage(6);
			return;
		}
		final String name = mdmt.getFileName(uploadFileUrl);
		//
		final IUploadCallback uploadCallback = new IUploadCallback() {
			
			@Override
			public void onSuccess(FileInfoResponseV2 successItem) {
				Message message = new Message();
				message.what = 3;
				message.obj = uploadFileUrl;
				Bundle bundle = new Bundle();
				bundle.putString("folderID", folderID);
				bundle.putString("fileID", successItem.getId());
				bundle.putString("filePath", uploadFileUrl);
				bundle.putString("fileEncPath", fileEncPath);
				message.setData(bundle);
				//
				mNotification.setContentText(name +"  "+    context.getResources().getString(R.string.fileListadapter_upload_success));
				mNotificationManager.notify(notificationID, mNotification.build());
				mNotificationManager.cancel(notificationID);
				handler.sendMessage(message);
			} 
			@Override
			public void onStart() {
				mNotification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon_logo)
						  .setContentTitle(name)
						  .setContentText(context.getResources().getString(R.string.upload_Notification_title));
						mNotification.setAutoCancel(false);
						mNotification.setTicker(context.getResources().getString(R.string.upload_Notification_title));
						Intent intent = new Intent(Intent.ACTION_MAIN);
						intent.addCategory(Intent.CATEGORY_LAUNCHER);
						intent.setClass(context, LoginActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
						PendingIntent p=PendingIntent.getActivity(context, 0, intent, 0);
						mNotification.setContentIntent(p);
						//
						mNotification.setProgress(100, (int)0, false);
						mNotification.setContentText(context.getResources().getString(R.string.upload_Notification_title)+"    "+(int)0+"%");
						mNotificationManager.notify(notificationID, mNotification.build());
					}
			@Override
			public void onProgress(int currentProgress, long currentSize, long maxSize) {
				mNotification.setContentTitle(name);
				mNotification.setProgress(100, (int)currentProgress, false);
				mNotification.setContentText(context.getResources().getString(R.string.upload_Notification_title)+"    "+(int)currentProgress+"%");
				mNotificationManager.notify(notificationID, mNotification.build());
			}
			@Override
			public void onFailure(Throwable t) {
				Message message = new Message();
				message.what = 4;
				handler.sendMessage(message);
				mNotification.setContentText(name +"  "+    context.getResources().getString(R.string.fileListadapter_upload_failed));
				mNotificationManager.notify(notificationID, mNotification.build());
				mNotificationManager.cancel(notificationID);
			}
			@Override
			public void onStop() {
			}
			@Override
			public void onCanceled() {
			}
			@Override
			public void onDettach() {
			}
		};
		Thread thread = new Thread()
		{
			@Override
			public void run()
			{
				try 
				{
					if (null != shareDriveService)
						shareDriveService.uploadFile(folderID ,uploadFileUrl,fileEncPath,inputStream,uploadCallback);
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
				super.run();
			}
		};
		thread.start();
	}
	public void uploadShareFile(String folderID,final String uploadFileUrl,final Handler handler)
	{
		this.folderID = folderID;
		File file = new File(uploadFileUrl);
		if (!file.exists()) 
		{
			handler.sendEmptyMessage(5);
			return;
		}
		final String name = file.getName();
		//
		IUploadCallback uploadCallback = new IUploadCallback() 
		{
			@Override
			public void onSuccess(FileInfoResponseV2 successItem) 
			{
				mNotification.setContentText(name +"  "+    context.getResources().getString(R.string.fileListadapter_upload_success));
				mNotificationManager.notify(notificationID, mNotification.build());
//				handler.sendMessage(message);
				mNotificationManager.cancel(notificationID);
				Message message = new Message();
				message.what = 1003;
				Bundle bundle = new Bundle();
				bundle.putString("folderID", DataUploadUtil.this.folderID);
				bundle.putString("fileID", successItem.getId());
				bundle.putString("filePath", uploadFileUrl);
				message.setData(bundle);
				handler.sendMessage(message);
			} 
			@Override
			public void onStart() 
			{
				mNotification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon_logo)
				  .setContentTitle(name)
				  .setContentText(context.getResources().getString(R.string.upload_Notification_title));
				mNotification.setAutoCancel(false);
				mNotification.setTicker(context.getResources().getString(R.string.upload_Notification_title));
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_LAUNCHER);
				intent.setClass(DataUploadUtil.this.context, LoginActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				PendingIntent p=PendingIntent.getActivity(context, 0, intent, 0);
				mNotification.setContentIntent(p);
				//
				mNotification.setProgress(100, (int)0, false);
				mNotification.setContentText(context.getResources().getString(R.string.upload_Notification_title)+"    "+(int)0+"%");
				mNotificationManager.notify(notificationID, mNotification.build());
			}
			@Override
			public void onProgress(int currentProgress, long currentSize, long maxSize) 
			{
//				share_notification_opertaing_title
				mNotification.setContentTitle(name);
				mNotification.setProgress(100, (int)currentProgress, false);
				mNotification.setContentText(context.getResources().getString(R.string.upload_Notification_title)+"    "+(int)currentProgress+"%");
				mNotificationManager.notify(notificationID, mNotification.build());
			}
			
			@Override
			public void onFailure(Throwable t)
			{
				Message message = new Message();
				message.what = 4;
				Bundle bundle = new Bundle();
				bundle.putString("uploadFilename", name);
				message.obj = bundle;
				handler.sendMessage(message);
				mNotification.setContentText(name +"  "+    DataUploadUtil.this.context.getResources().getString(R.string.fileListadapter_upload_failed));
				mNotificationManager.notify(notificationID, mNotification.build());
				mNotificationManager.cancel(notificationID);
			}
			@Override
			public void onStop() {
			}
			@Override
			public void onCanceled() {
			}
			@Override
			public void onDettach() {
			}
		};
		notificationID =ClientConfig.NOTIFICATION_ID+=1;
		if(folderID !=null && !folderID.equals(""))
		{
			if (null != shareDriveService)
				shareDriveService.uploadFile(folderID ,uploadFileUrl, uploadCallback);
		} 
		else 
		{
			folderID = "0";
			if (null != shareDriveService)
				shareDriveService.uploadFile(folderID ,uploadFileUrl,uploadCallback);
		}
	
	}
}
